package com.egao.common.module.world.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cms.entity.Link;
import com.egao.common.module.cms.mapper.LinkMapper;
import com.egao.common.module.cms.service.LinkService;
import com.egao.common.module.world.entity.WorldGameLink;
import com.egao.common.module.world.mapper.WorldGameLinkMapper;
import com.egao.common.module.world.service.WorldGameLinkService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 友情链接实现类
 * @author pyx
 */
@Service
public class WorldGameLinkServiceImpl extends ServiceImpl<WorldGameLinkMapper, WorldGameLink> implements WorldGameLinkService {

    @Override
    public List<WorldGameLink> WorldGameListPage(PageParam<WorldGameLink> pageParam) {
        return baseMapper.WorldGameListPage(pageParam);
    }

    @Override
    public JsonResult getId(Integer id) {
        if(id == null){
            throw new ParameterException("参数对象为空");
        }

        QueryWrapper<WorldGameLink> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Integer count = baseMapper.selectCount(wrapper);

        if(count != 1){
            throw new ParameterException("链接不存在");
        }

        WorldGameLink worldGameLink = baseMapper.selectById(id);
        return JsonResult.ok().setData(worldGameLink);
    }

    @Override
    public JsonResult insertWorldGameLink(WorldGameLink worldGameLink) {
        if(worldGameLink == null){
            throw new ParameterException("参数对象为空");
        }

        if(StrUtil.isBlank(worldGameLink.getTitle())){
            throw new ParameterException("链接标题不能为空");
        }

        baseMapper.insert(worldGameLink);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateWorldGameLink(WorldGameLink worldGameLink) {
        if(worldGameLink == null){
            throw new ParameterException("参数对象为空");
        }

        if(StrUtil.isBlank(worldGameLink.getTitle())){
            throw new ParameterException("链接标题不能为空");
        }

        if(worldGameLink.getSort() == null){
            worldGameLink.setSort(9999);
        }

        baseMapper.updateById(worldGameLink);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult deleteById(Integer id) {
        if(id == null){
            throw new ParameterException("参数对象为空");
        }

        if(removeById(id)){
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }
}
